package com.ruimei.service.impl;

import com.ruimei.entitys.Clock;
import com.ruimei.entitys.Teacher;
import com.ruimei.mapper.ClockMapper;
import com.ruimei.service.ClockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClockServiceImpl implements ClockService {

    @Resource
    ClockMapper mapper;

    @Override
    public Map<String, Object> addClock(Clock clock) {
        //判断是否被打卡了
        Map<String,Object> clockMap = new HashMap<>();
        clockMap.put("classId",clock.getClassId());
        clockMap.put("type",clock.getType());
        clockMap.put("time",clock.getTime());


        List<Clock> clocks = mapper.selectClock(clockMap);
        Map<String, Object> map = new HashMap();

        if(clocks.isEmpty() || "OTH".equals(clock.getType()) ){  //判断是否打卡  或者打卡内容是其他
            //设置 打卡信息
            clock.setId(new Date().getTime() + "");
            clock.setCreateTime(new Date());
            Integer i = mapper.inserClock(clock);
            if(i.intValue() == 1){
                map.put("status", Boolean.valueOf(true));
                map.put("message", "添加成功");
            }else{
                map.put("status", Boolean.valueOf(false));
                map.put("message", "添加失败");
            }

        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "该时段"+clocks.get(0).getTeacherName()+"老师已打卡");
        }


        return map;
    }

    @Override
    public Map<String, Object> getClock(String id,String type,String classId,String teacherName,String btime,String etime) {
        Map<String, Object> map = new HashMap();

        Map<String,Object> clockMap = new HashMap<>();
        clockMap.put("id",id);
        clockMap.put("classId",classId);
        clockMap.put("type",type);
        clockMap.put("teacherName",teacherName);
        clockMap.put("btime",btime);
        clockMap.put("etime",etime);


        List<Clock> list = mapper.selectClock(clockMap);

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;

    }

    @Override
    public Map<String, Object> deleteClock(Clock clock) {
        Map<String, Object> map = new HashMap();
        Integer i = mapper.deleteClock(clock);
        if(i.intValue() == 1){
            map.put("status", Boolean.valueOf(true));
            map.put("message", "删除成功");
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "删除失败");
        }
        return map;
    }
}
